package com.example.kotlin.activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.example.kotlin.R
import com.example.kotlin.room.AppDatabase
import com.example.kotlin.room.entity.User
import kotlinx.android.synthetic.main.activity_room.addDataBtn
import kotlinx.android.synthetic.main.activity_room.deleteDataBtn
import kotlinx.android.synthetic.main.activity_room.queryDataBtn
import kotlinx.android.synthetic.main.activity_room.updateDataBtn
import kotlin.concurrent.thread

class RoomActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_room)

        val userDao = AppDatabase.getDatabase(this).userDao()
        val user1 = User("Tom", "Brady", 40)
        val user2 = User("Tom", "Hanks", 63)
        val user3 = User("张之维", "绝顶一人", 106)
        addDataBtn.setOnClickListener {
            thread {
                user1.id = userDao.insertUser(user1)
                user2.id = userDao.insertUser(user2)
                user3.id = userDao.insertUser(user3)
            }
        }

        updateDataBtn.setOnClickListener {
            thread {
                user1.age=300
                userDao.updateUser(user1)
            }
        }

        deleteDataBtn.setOnClickListener {
            thread {
                userDao.deleteUserByLastName("Hanks")
            }
        }

        queryDataBtn.setOnClickListener {
            thread {
                for (user in userDao.loadAllUsers()){
                    Log.e("mainactivty", user.toString())
                }
            }
        }

    }
}